package cn.easier.activity.console.controller;

import cn.easier.activity.console.entity.BargainGoods;
import cn.easier.activity.console.entity.GoodsType;
import cn.easier.activity.console.service.ActivityBargainService;
import cn.easier.club.base.dto.ResultInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/activityBargain")
public class ActivityBargainController {
    private static Logger logger = LoggerFactory.getLogger(ActivityBargainController.class);

    private final ActivityBargainService activityBargainService;

    public ActivityBargainController(ActivityBargainService activityBargainService) {
        this.activityBargainService = activityBargainService;
    }

    /**
     * 查看商品列表
     *
     * @return
     */
    @RequestMapping(value = "/getBargainMessage", method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo getBargainMessage(@RequestParam(value = "activityId", required = true) Integer activityId,
                                        @RequestParam(value = "pageNum", required = true) int pageNum,
                                        @RequestParam(value = "pageSize", required = true) int pageSize) {
        List<Object> result = activityBargainService.getMessage(activityId, pageNum, pageSize);
        Object o = result.get(result.size() - 1);
        result.remove(result.size() - 1);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), String.valueOf(o), result);
    }

    /**
     * 添加商品相关信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/addBargainMessage")
    public ResultInfo addBargainMessage(@RequestBody BargainGoods params) {
        ResultInfo resultInfo = null;
        try {
            BargainGoods returnActivityInfo = activityBargainService.updateBargainMessage(params);
            logger.info("商品添加修改成功: =>{}", returnActivityInfo);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "商品修改或添加成功", returnActivityInfo);
        } catch (Exception e) {
            e.printStackTrace();
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "商品创建或编辑异常", null);
            logger.error("商品添加修改异常,BargainGoods: =>{}", params);
        }
        return resultInfo;
    }

    /**
     * 删除指定砍价商品
     *
     * @param id 商品编号
     * @return
     */
    @DeleteMapping("/id/{id}")
    @ResponseBody
    public Map<String, String> delete(@PathVariable Integer id) {
        // todo 如果当前用户不存在，返回异常
        Boolean deleteFlag = false;
        ResultInfo resultInfo = null;
        try {
            deleteFlag = activityBargainService.delete(id);
            logger.info("删除指定活动：=>{}", deleteFlag ? "成功" : "失败");
        } catch (Exception e) {
            logger.info("删除指定活动异常,id：=>{}", id);
        }
        if (deleteFlag) {
            resultInfo = activityBargainService.deletePrizeMessage(id);
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("deleteFlag", deleteFlag ? "true" : "false");
        map.put("message", resultInfo.getDescription());
        return map;
    }

    /**
     * 编辑砍价商品库存
     *
     * @param id 商品编号
     * @return
     */
    @PostMapping("/updateGoodsStock/{id}")
    @ResponseBody
    public ResultInfo updateGoodsStock(@PathVariable Integer id, @RequestParam(value = "goodsStock", required = true) int goodsStock) {
        // todo 如果当前用户不存在，返回异常
        ResultInfo resultInfo = null;
        try {
            ResultInfo returnActivityInfo = activityBargainService.updateGoodsStock(id, goodsStock);
            logger.info("商品库存修改成功: =>{}", returnActivityInfo);
            return returnActivityInfo;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("商品库存修改异常,BargainGoods: =>{}", goodsStock);
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "砍价商品库存修改异常", null);
        }
    }

    /**
     * 商品上下架
     *
     * @param id   商品Id
     * @param flag 是否上架
     * @return
     */
    @PostMapping("/id/{id}/run-or-pause")
    @ResponseBody
    public ResultInfo runOrPause(@PathVariable Integer id, Boolean flag) {
        // todo 如果当前用户不存在，返回异常
        ResultInfo resultInfo = null;
        if (flag == null) {
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "操作有误，请核实信息。", null);
        }
        Boolean returnFlag = false;
        try {
            BargainGoods bargainGoods = activityBargainService.findByOne(id);
            if (bargainGoods == null) {
                logger.info("指定商品上下架失败，没有找到活动, id: =>{}", id);
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "活动上下架失败，没有找到活动",
                        null);
            }
            returnFlag = activityBargainService.runOrPauseOrSuperUser(flag, id);
            logger.info("指定商品" + (flag ? "上" : "下") + "架，执行完成：=>{}, id: =>{}", returnFlag ? "成功" : "失败", id);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK),
                    "商品" + (flag ? "上" : "下") + "架" + (returnFlag ? "成功" : "失败"), null);
        } catch (Exception e) {
            logger.info("商品上下架异常,id：=>{}", id);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "商品上下架异常", null);
        }
        return resultInfo;
    }

    /**
     * 查看商品类型列表
     *
     * @return
     */
    @RequestMapping(value = "/getGoodsTypeMessage", method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo getGoodsTypeMessage(@RequestParam(value = "pageNum", required = true) int pageNum,
                                          @RequestParam(value = "pageSize", required = true) int pageSize) {
        List<Object> result = activityBargainService.getGoodsTypeMessage(pageNum, pageSize);
        Object o = result.get(result.size() - 1);
        result.remove(result.size() - 1);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), String.valueOf(o), result);
    }

    /**
     * 查看商品类型下拉框
     *
     * @return
     */
    @RequestMapping(value = "/getGoodsTypeSelect", method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo getGoodsTypeSelect() {
        List<Object> result = activityBargainService.getGoodsTypeSelect();
        Object o = result.get(result.size() - 1);
        result.remove(result.size() - 1);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), String.valueOf(o), result);
    }

    /**
     * 删除指定商品类型
     *
     * @param id 商品类型编号
     * @return
     */
    @DeleteMapping("/delType/{id}")
    @ResponseBody
    public Map<String, String> deleteGoodsType(@PathVariable Integer id) {
        // todo 如果当前用户不存在，返回异常
        Boolean deleteFlag = false;
        try {
            deleteFlag = activityBargainService.deleteGoodsType(id);
            logger.info("删除指定商品类型：=>{}", deleteFlag ? "成功" : "失败");
        } catch (Exception e) {
            logger.info("删除指定商品类型异常,id：=>{}", id);
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("deleteFlag", deleteFlag ? "true" : "false");
        return map;
    }

    /**
     * 添加修改商品类型相关信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updateGoodsTypeMessage")
    public ResultInfo updateGoodsTypeMessage(@RequestBody @Valid GoodsType params) {
        ResultInfo resultInfo = null;
        try {
            GoodsType returnActivityInfo = activityBargainService.updateGoodsTypeMessage(params);
            logger.info("商品类型添加修改成功: =>{}", returnActivityInfo);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "商品类型修改或添加成功", returnActivityInfo);
        } catch (Exception e) {
            e.printStackTrace();
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "商品类型创建或编辑异常", null);
            logger.error("商品类型添加修改异常,BargainGoods: =>{}", params);
        }
        return resultInfo;
    }

    /**
     * 查看订单列表
     *
     * @return
     */
    @RequestMapping(value = "/getGoodsOrderMessage", method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo getGoodsOrderMessage(@RequestParam(value = "pageNum", required = true) int pageNum,
                                           @RequestParam(value = "pageSize", required = true) int pageSize,
                                           @RequestParam(value = "activityId", required = false) Integer activityId,
                                           @RequestParam(value = "phoneNum", required = false) String phoneNum,
                                           @RequestParam(value = "bargainOrderNo", required = false) String bargainOrderNo) {
        List<Object> result = activityBargainService.getGoodsOrderMessage(pageNum, pageSize, activityId, phoneNum, bargainOrderNo);
        Object o = result.get(result.size() - 1);
        result.remove(result.size() - 1);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), String.valueOf(o), result);
    }

    /**
     * 存储或者修改wap图片
     *
     * @return
     */
    @RequestMapping(value = "/saveWapImg", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo saveWapImg(@RequestParam(value = "url", required = true) String url, @RequestParam(value = "activityId", required = false) Integer activityId,
                                 @RequestParam(value = "linkUrl", required = true) String linkUrl, @RequestParam(value = "id", required = false) Integer id) {
        return activityBargainService.saveWapImg(url, activityId, linkUrl, id);
    }

    /**
     * 查看广告位图片列表
     *
     * @return
     */
    @RequestMapping(value = "/getAdvertisingPosition", method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo getAdvertisingPosition(@RequestParam(value = "activityId", required = false) Integer activityId,
                                             @RequestParam(value = "pageNum", required = true) int pageNum,
                                             @RequestParam(value = "pageSize", required = true) int pageSize) {
        List<Object> result = activityBargainService.getAdvertisingPosition(activityId, pageNum, pageSize);
        Object o = result.get(result.size() - 1);
        result.remove(result.size() - 1);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), String.valueOf(o), result);
    }

    /**
     * 获得广告位图片活动列表
     *
     * @return
     */
    @RequestMapping(value = "/getActivityList", method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo getActivityList() {
        return activityBargainService.getActivityList();
    }

    /**
     * 删除指定广告位图片
     *
     * @param id 广告位编号
     * @return
     */
    @DeleteMapping("/delAdvertisingPosition/{id}")
    @ResponseBody
    public Map<String, String> delAdvertisingPosition(@PathVariable Integer id) {
        // todo 如果当前用户不存在，返回异常
        Boolean deleteFlag = false;
        try {
            deleteFlag = activityBargainService.delAdvertisingPosition(id);
            logger.info("删除指定广告位图片：=>{}", deleteFlag ? "成功" : "失败");
        } catch (Exception e) {
            logger.info("删除指定广告位图片异常,id：=>{}", id);
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("deleteFlag", deleteFlag ? "true" : "false");
        return map;
    }

    /**
     * 置顶指定广告位图片
     *
     * @param id 广告位编号
     * @return
     */
    @RequestMapping(value = "/topFlagAdvertisingPosition/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo topFlagAdvertisingPosition(@PathVariable Integer id, Integer status) {
        ResultInfo message = activityBargainService.topFlagAdvertisingPosition(id, status);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "保存成功", message);
    }
}


